package com.yl.zqy.service.impl.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yl.zqy.common.Result;
import com.yl.zqy.dto.SysRoleUserDTO;
import com.yl.zqy.entity.sys.SysRole;
import com.yl.zqy.mapper.sys.SysRoleMapper;
import com.yl.zqy.service.sys.SysRoleMenuService;
import com.yl.zqy.service.sys.SysRoleService;
import com.yl.zqy.service.sys.SysRoleUserService;
import com.yl.zqy.service.sys.SysUserService;
import com.yl.zqy.utils.QueryUtil;
import com.yl.zqy.vo.common.IPageVo;
import com.yl.zqy.vo.user.AuthMenusByRole;
import com.yl.zqy.vo.user.AuthUsersByRole;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色管理 服务实现类
 * </p>
 *
 * @author liang.yu
 * @since 2024-03-28
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    private final SysUserService userService;

    private final SysRoleUserService roleUserService;

    private final SysRoleMenuService menuService;

    @Override
    public Result<Boolean> insertRole(SysRole role) {
        int res = baseMapper.insert(role);
        if (res > 0)
            return Result.ok(true);
        return Result.error("新增失败！");
    }

    @Override
    public Result<Boolean> updateRole(SysRole role) {
        int res = baseMapper.updateById(role);
        if (res > 0)
            return Result.ok(true);
        return Result.error("修改失败！");
    }

    @Override
    public Result<Boolean> updateRoleStatus(SysRole role) {
        SysRole entity = new SysRole();
        entity.setId(role.getId());
        entity.setStatus(role.getStatus());
        int res = baseMapper.updateById(entity);
        if (res > 0)
            return Result.ok(true);
        return Result.error("修改失败！");
    }

    @Override
    public Result<IPageVo<SysRole>> roleByPage(int current, int size, String status, String query) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(StringUtils.isNotBlank(status), c -> c.eq(StringUtils.isNotBlank(status), SysRole::getStatus, status))
                .and(StringUtils.isNotBlank(query), c -> c.like(StringUtils.isNotBlank(query), SysRole::getName, query)
                        .or()
                        .like(StringUtils.isNotBlank(query), SysRole::getRoleCode, query));
        Page<SysRole> sysRolePage = baseMapper.selectPage(Page.of(current, size), queryWrapper);
        return Result.ok(QueryUtil.iPage2IPageVo(sysRolePage));
    }

    @Override
    public Result<List<SysRoleUserDTO>> getRoleNoAuthorizeByUsers(String roleId, String query) {
        List<SysRoleUserDTO> list = userService.getUserNotAuthByRoleId(roleId, query);
        return Result.ok(list);
    }

    @Override
    public Result<List<SysRoleUserDTO>> getRoleAuthorizeByUsers(String roleId, String query) {
        List<SysRoleUserDTO> list = userService.getUserAuthByRoleId(roleId, query);
        return Result.ok(list);
    }

    @Override
    public Result<Boolean> roleAuthorizeUsers(AuthUsersByRole auth) {
        return Result.ok(roleUserService.roleAuthorizeUsers(auth));
    }

    @Override
    public Result<Boolean> roleAuthorizeMenus(AuthMenusByRole auth) {
        return Result.ok(menuService.roleAuthorizeMenus(auth));
    }

    @Override
    public Result<List<String>> getRoleByMenus(String roleId) {
        return Result.ok(menuService.getRoleByMenus(roleId));
    }

    @Override
    public List<String> getUserByRole(Object loginId) {

        return baseMapper.getUserByRole(loginId.toString());
    }
}
